import openpyxl

def read_sheet():
    '''桌牌内容输入'''
    print(f"请输入桌牌名称：\n(以 end 结束输入)")
    while True:
        user_input = input()
        if user_input.lower() == 'end':
            break
        if not user_input:  # 检查用户输入是否为空
            continue
        words_list.append(user_input)

if __name__ == '__main__':
    words_list = []
    read_sheet()

    # 创建新的工作簿sheet
    workbook = openpyxl.Workbook()
    for item in words_list:
        if item == 'end':
            break
        worksheet = workbook.create_sheet(title=item)

        # 桌牌固定excel单元格大小设置
        worksheet.column_dimensions['A'].width = int(13)
        worksheet.column_dimensions['B'].width = int(51)
        worksheet.column_dimensions['C'].width = int(51)
        worksheet.column_dimensions['D'].width = int(13)

        for i in range(1, 18):
            worksheet.row_dimensions[i].height = int(31)

        # 桌牌文本字体格式设置
        font1 = openpyxl.styles.Font(name='华文新魏', size=151)
        font2 = openpyxl.styles.Font(name='华文新魏', size=156)
        font3 = openpyxl.styles.Font(name='华文新魏', size=110)
        font4 = openpyxl.styles.Font(name='华文新魏', size=85)
        style_1 = openpyxl.styles.Alignment(text_rotation=180, vertical='center', horizontal='center', wrap_text=True)
        style_2 = openpyxl.styles.Alignment(text_rotation=90, vertical='center', horizontal='center', wrap_text=True)

        # 合并单元格和填充内容
        worksheet.merge_cells('B1:B17')
        worksheet.merge_cells('C1:C17')

        worksheet['B1'].value = item
        worksheet['B1'].alignment = style_1
        
        worksheet['C1'].value = item
        worksheet['C1'].alignment = style_2
        
        if len(item) == 3:      # 三字姓名特殊处理
            worksheet['B1'].font =worksheet['C1'].font = font1
        elif len(item) == 2 or (len(item) == 4 and item.isdigit()):   # 两字姓名 & 四位数字的团支部
            worksheet['B1'].font =worksheet['C1'].font = font2
        elif len(item) == 6 and item[-4:].isdigit():    # 四字姓名 & 正常团支部格式
            worksheet['B1'].font =worksheet['C1'].font = font3
        elif item[2:6].isdigit():           # 团支部 + 姓名
            item = item[:6] + '    ' + item[6:]
            worksheet['B1'].value =worksheet['C1'].value = item
            worksheet['B1'].font =worksheet['C1'].font = font4
        else:       # 其他情况自行调整
            worksheet['B1'].font =worksheet['C1'].font = font4

        border_style = openpyxl.styles.Border(
            left=openpyxl.styles.Side(border_style='thin', color='000000'),
            right=openpyxl.styles.Side(border_style='thin', color='000000'),
        )
        for row in worksheet.iter_rows(min_row=1, max_row=17, min_col=2, max_col=3):
            for cell in row:
                cell.border = border_style

        # 打印设置
        worksheet.print_area = 'A1:D17'
        worksheet.page_setup.orientation=worksheet.ORIENTATION_LANDSCAPE
        worksheet.page_setup.paperSize = worksheet.PAPERSIZE_A4

    # 删除多余的默认Sheet
    worksheet = workbook['Sheet']
    workbook.remove(worksheet)

    new_file_name = input("对生成文件进行命名：\n(跳过将默认为card.xlsx)")
    if not new_file_name:
        new_file_name = 'card'
    workbook.save(new_file_name + '.xlsx')